//
//  BannerCell.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "BannerCell.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
//#define HEIGHT 200
@implementation BannerCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myBanners = [NSMutableArray array];
        //创建布局
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        // 720    320
        CGFloat bili = 320 / 720.0;
        //item大小
        flow.itemSize = CGSizeMake(WIDTH, WIDTH * bili);
        //最小列间距
        flow.minimumInteritemSpacing = 0;
        flow.minimumLineSpacing = 0;
        //滚动方向
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.bannerCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH * bili) collectionViewLayout:flow];
        [self.contentView addSubview:self.bannerCollection];
        self.bannerCollection.pagingEnabled = YES;
        self.bannerCollection.bounces = NO;
        [self.bannerCollection release];
        self.bannerCollection.delegate = self;
        self.bannerCollection.dataSource = self;
        
        //注册cell
        [self.bannerCollection registerClass:[BannerCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        
        self.myPageControl = [[UIPageControl alloc] init];
        self.myPageControl.numberOfPages = [self.bannerCount integerValue];
        self.myPageControl.frame = CGRectMake(0, 0, WIDTH / 2, 30);
        self.myPageControl.center = CGPointMake(WIDTH / 2, WIDTH * bili - 20);
        [self.contentView addSubview:self.myPageControl];
        [self.myPageControl release];
        self.myPageControl.pageIndicatorTintColor = [UIColor blackColor];
        self.myPageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [self.myPageControl addTarget:self action:@selector(myPageControllerEvent:) forControlEvents:UIControlEventValueChanged];
        //调用定时器
        [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(bannerAuto) userInfo:nil repeats:YES];
        
    }
    return self;
}

//点击的实现
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    banner *b = self.myBanners[indexPath.row];
    [self.delegate getSelectedBannerIndex:b.comic_id];
}

- (void)bannerAuto{
    NSInteger index = self.myPageControl.currentPage;
    if (index == 2) {
        index = 0;
    }else{
        index++;
    }
    self.myPageControl.currentPage = index;
    self.bannerCollection.contentOffset = CGPointMake(index * WIDTH, 0);
}



- (void)myPageControllerEvent:(UIPageControl *)myPage{
    NSInteger page = myPage.currentPage;
    self.bannerCollection.contentOffset = CGPointMake(WIDTH * page, 0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    self.myPageControl.numberOfPages = [self.bannerCount integerValue];
    return [self.bannerCount integerValue];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSURL *url = [NSURL URLWithString:[self.myBanners[indexPath.row] banner_url]];
    UIImage *placeImage = [UIImage imageNamed:@"FOR@{XC4(JCI7}{P@Y]({{1－1（被拖移）.tiff"];
    [cell.bannerImage sd_setImageWithURL:url placeholderImage:placeImage];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = self.bannerCollection.contentOffset.x / WIDTH;
    self.myPageControl.currentPage = index;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
